<template>
  <div class="warpper-box sendSalary">
    <enterprise-select v-if="userInfo.type == 4" ref="enterprise" @enterpriseChange="enterpriseChange"></enterprise-select>
    <a-card style="margin-top: 10px;">
      <div style="display: flex;justify-content: flex-start;align-items: flex-start;">
        <form-create v-model="params.fApi" :rule="params.rule" :option="params.option" @btn-search-click="loadTable(true)" @btn-reset-click="reset" />
      </div>
    </a-card>
    <a-card style="margin-top: 10px;">
      <a-table :columns="table.columns" :data-source="table.loadData" :pagination="table.pagination"
        @change="handleTableChange" rowKey="orderId" :scroll="{ x: 2800 }">
        <!-- 操作栏-->
        <span slot="action" slot-scope="text, record">
          <!-- 编辑模块 | 字段 -->
          <span v-if="record.settleStatus < 2">
            <a-popconfirm v-show="record.isOnline == 1" title="是否确认支付？" ok-text="确认" cancel-text="取消" @confirm="isResolve(record)">
              <a>支付</a>
            </a-popconfirm>
            <a @click="isPayOrder(record)" v-show="record.isOnline == 2">支付</a>
            <a-divider type="vertical" />
            <a-popconfirm title="是否确认拒绝？" ok-text="确认" cancel-text="取消" @confirm="rejectHandle(record)">
              <a>拒绝</a>
            </a-popconfirm>
            <a-divider type="vertical" />
          </span>
          <span v-else>
            <a @click="salaryDetail(record)">详情</a>
          </span>
        </span>
      </a-table>
    </a-card>
    <!-- 支付审核 -->
    <a-modal v-model="isPay" title="支付审核确认" :confirmLoading="submitLoading" width="500px"
      ok-text="确认" cancel-text="取消" @ok="resolveHandle">
      <a-space style="padding-left: 80px;">
        业务类型：
        <a-select v-model="busCod" placeholder="请选择" style="width: 200px;">
          <a-select-option key="N03010">
            代发工资
          </a-select-option>
          <a-select-option key="N03020">
            代发其他
          </a-select-option>
        </a-select>
      </a-space>
      <a-space style="padding-left: 80px;padding-top: 10px;">
        业务模式：
        <a-select v-model="busMod" placeholder="请选择业务模式" style="width: 200px;">
          <a-select-option v-for="item in workList" :key="item.busmod">
            {{item.modals}}
          </a-select-option>
        </a-select>
      </a-space>
    </a-modal>
  </div>
</template>

<script>
import store from '@/store'
import EnterpriseSelect from "@/components/human/enterpriseSelect.vue"
import {
  PARAMS_SALARYBILL,
  COLUMNS_SENDSALARY
} from "./form"
import {
  OPTION_BLOCK,
  OPTION_INLINE
} from "../base_form"
import {
  getTablePage,
  orderPay,
  rejectOrder,
  cmbDclismod
} from "@/api/human/salaryPaySendSalary"
import { mapGetters, mapActions } from 'vuex'
export default {
  name: 'sendSalaryBill',
  components: {
    EnterpriseSelect
  },
  data() {
    return {
      params: {
        fApi: {},
        rule: PARAMS_SALARYBILL,
        option: OPTION_INLINE,
      },
      table: {
        columns: COLUMNS_SENDSALARY,
        loadData: [],
        pagination: {
          current: 1,
          pageSize: 10,
          total: 0,
          showSizeChanger: true,
					showQuickJumper: true,
          pageSizeOptions: ["10", "20", "50", "100"],
          showTotal: total => `共有 ${total} 条数据`,
        }
      },
      pagination: { current: 1, pageSize: 10 },

      enterpriseId: "", // 企业id
      enterpriseData: [], // 客户列表

      isPay: false, // 支付订单开关
      submitLoading: false,
      orderId: "",
      busCod: "", // 业务类型
      busMod: undefined, // 业务模式
      workList: [], // 业务模式列表

      selectedRowKeys: [], // 选择的table数据id
      selectedRows: [], // 选择的table数据
    }
  },
  filters: {
    filterMore(value, num) {
      if (!value) return ''
      if (value.length > num) {
        return value.slice(0, num) + '...'
      }
      return value
    }
  },
  mounted() {
    window.addEventListener('keyup', this.keyDown);
    if (this.userInfo.type == 4) {
      this.$refs.enterprise.getEnterpriseData();
    } else {
      this.enterpriseId = this.userInfo.relationId
      this.loadTable(true);
    }
  },
  destroyed() {
    window.removeEventListener('keyup', this.keyDown);
  },
  beforeCreate() {
    this.events_form = this.$form.createForm(this, {
      name: 'events_form'
    })
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  created() {

  },
  methods: {
    // table复选框选择
    onSelectChange(selectedRowKeys, selectedRows) {
      // console.log('selectedRowKeys changed: ', selectedRowKeys);
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
    },
    // 获取企业员工列表
    async loadTable(flag) {
      let params = this.params.fApi.formData();
      if (flag) {
        this.table.pagination.current = 1;
        this.table.pagination.pageSize = 10;
      }
      params.enterpriseId = this.enterpriseId;
      params.pageNum = this.table.pagination.current;
      params.pageSize = this.table.pagination.pageSize;
      const { code, data } = await getTablePage(params);
      if (code === '1') {
        this.table.loadData = data.content;
        this.table.pagination.total = data.total || 0;
      }
    },
    handleTableChange(pagination) {
      this.table.pagination.current = pagination.current;
      this.table.pagination.pageSize = pagination.pageSize;
      this.loadTable(false);
    },
    // 发薪明细
    salaryDetail(row) {
      this.$router.push({ name: 'humanResourcesSendSalaryDetailCode', query: { enterpriseId:this.enterpriseId,orderId: row.orderId }});
    },
    // 支付订单
    isPayOrder(row) {
      // 线上
      this.isPay = true;
      this.orderId = row.orderId;
      this.busCod = "N03010";
      this.busMod = undefined;
      this.getWorkType();
    },
    isResolve(row) {
      // 线下
      this.orderId = row.orderId;
      this.busCod = undefined;
      this.busMod = undefined;
      this.resolveHandle();
    },
    async resolveHandle() {
      let params = {
        orderId: this.orderId,
        busCod: this.busCod,
        busMod: this.busMod
      }
      const { code } = await orderPay(params);
      if (code === '1') {
        this.$message.success('操作成功');
        this.isPay = false;
        this.loadTable(true);
      }
    },
    // 拒绝订单
    async rejectHandle(row) {
      let params = {
        orderId: row.orderId
      }
      const { code } = await rejectOrder(params);
      if (code === '1') {
        this.$message.success('操作成功');
        this.loadTable(true);
      }
    },
    // 获取业务类型
    async getWorkType() {
      const { code,data } = await cmbDclismod(this.orderId);
      if (code === '1') {
        this.workList = data
      }
    },
    // 企业选择回调
    enterpriseChange(value) {
      this.enterpriseId = value;
      this.loadTable(true);
    },
    addLoad() {
      this.loadTable(true);
    },
    reset() {
      this.params.fApi.resetFields();
      this.loadTable(true);
    }
  }
}
</script>
<style lang="less"></style>